//
//  feedBackViewController.m
//  唧歪
//
//  Created by ZC on 15/7/9.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "feedBackViewController.h"
#import "SetUpViewController.h"
#import "AppDelegate.h"
#define feedbackURL @"http://192.168.1.103:8888/index.php/JiWai/feedback"

@interface feedBackViewController (){
    AppDelegate *app;
    NSString *feedBackStr;
    UIAlertView *_alert;
}

@end

@implementation feedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"意见反馈";
    self.submitButton.layer.masksToBounds = YES;
    self.submitButton.layer.cornerRadius = 5;
    app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)blackButton:(id)sender {
    
    [self.feedBackText resignFirstResponder];
}


#pragma mark - 提交按钮事件

- (IBAction)submit:(id)sender {
    
    if ([self.feedBackText.text isEqualToString:@""]) {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"您还满意输入内容" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        
    }else{
        NSString *postr = [NSString stringWithFormat:@"user_id=%@&feedBack_text=%@",app.user_id,self.feedBackText.text];
        [self loadingPath:feedbackURL Str:postr];
        
    }
}

-(void)loadingPath:(NSString *)path Str:(NSString *)string{
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        feedBackStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:feedBackStr waitUntilDone:YES];
    }];
}

-(void)refreshData:(NSMutableArray *)sender{
    int str=[feedBackStr intValue];
    if (str == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提交失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提交成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        _alert=alert;
        [alert show];
    }
}
#pragma mark - 提示框“确定”事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0&&alertView==_alert) {
        //跳转到设置页面
        SetUpViewController *setUp = [self.storyboard instantiateViewControllerWithIdentifier:@"setUpVC"];
        [self.navigationController pushViewController:setUp animated:YES];
    }
}


@end
